package com.sks.web.action.privilege;

import java.io.File;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.stereotype.Controller;

import com.sks.bean.pojo.Users;
import com.sks.bean.privilege.Department;
import com.sks.bean.privilege.Employee;
import com.sks.bean.privilege.IDCard;
import com.sks.bean.privilege.PrivilegeGroup;
import com.sks.bean.user.Gender;
import com.sks.service.privilege.DepartmentService;
import com.sks.service.privilege.EmployeeService;
import com.sks.service.privilege.PrivilegeGroupService;
import com.sks.service.privilege.UsersService;
import com.sks.utils.SiteUrl;
import com.sks.web.formbean.privilege.EmployeeForm;
import com.sks.web.formbean.privilege.UsersForm;
/**
 * 外部员工管理
 */
@Controller("/cms/privilege/users/manage")
public class UsersManageAction extends DispatchAction {
	@Resource UsersService usersService;
	@Resource DepartmentService departmentService;
	@Resource PrivilegeGroupService groupService;
	
	/**
	 * 员工查询界面
	 */
	@Permission(module="users", privilege="view")
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setAttribute("departments", departmentService.getScrollData().getResultlist());
		return mapping.findForward("query");
	}
	
	
	/**
	 * 员工修改界面
	 */
	@Permission(module="users", privilege="update")
	public ActionForward editUsersUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {	
		UsersForm formbean = (UsersForm)form;
		Users users = usersService.find(formbean.getUserId());
		formbean.setFamilyName(users.getFamilyName());
		formbean.setSex(users.getSex());
		formbean.setEmail(users.getEmail());
		formbean.setUserId(users.getUserId());
		formbean.setLoginName(users.getLoginName());
		formbean.setMobile(users.getMobile());
		formbean.setStatus(users.getStatus());
		//request.setAttribute("imagePath", employee.getImagePath());
		//request.setAttribute("departments", departmentService.getScrollData().getResultlist());
		return mapping.findForward("edit");
	}
	
	 /**
	 * 修改网站用户信息
	 */
	@Permission(module="users", privilege="update")
	public ActionForward editUsers(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {	
		UsersForm formbean = (UsersForm)form;
		Users users = usersService.find(formbean.getUserId());
		users.setSex(formbean.getSex());
		users.setEmail(formbean.getEmail());
		users.setUserId(formbean.getUserId());
		users.setLoginName(formbean.getLoginName());
		users.setMobile(formbean.getMobile());
		users.setFamilyName(formbean.getFamilyName());
		usersService.update(users);
		request.setAttribute("message", "外部用户修改成功");		
		request.setAttribute("urladdress", SiteUrl.readUrl("sks.privilege.users.list"));
		return mapping.findForward("message");
	}
	/**
	 * 判断员工的用户名是否存在
	 */
	public ActionForward exist(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {	
		UsersForm formbean = (UsersForm)form;
		request.setAttribute("exist", usersService.exist(formbean.getLoginName()));
		return mapping.findForward("checkResult");
	}
	/**
	 * 员工添加界面
	 */
	@Permission(module="users", privilege="insert")
	public ActionForward regUsersUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {	
		UsersForm formbean = (UsersForm)form;
		formbean.setSex(Gender.MAN);
		//request.setAttribute("departments", departmentService.getScrollData().getResultlist());
		return mapping.findForward("add");
	}
	/**
	 * 添加外部员工
	 */
	@Permission(module="users", privilege="insert")
	public ActionForward regUsers(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {	
		UsersForm formbean = (UsersForm)form;
		Date regDate = new Date();
		Users users = new Users("", "", "");
		users.setLoginName(formbean.getLoginName().trim());
		users.setFamilyName(formbean.getFamilyName());
		users.setLoginPwd(formbean.getLoginPwd().trim());
		users.setSex(formbean.getSex());
		users.setEmail(formbean.getEmail());
		users.setMobile(formbean.getMobile());
		users.setStatus(true);
		users.setLastUpdateTime(regDate);
		users.setRegisterTime(regDate);
		usersService.save(users);
		request.setAttribute("message", "外部员工添加成功");		
		request.setAttribute("urladdress", SiteUrl.readUrl("sks.privilege.users.list"));
		return mapping.findForward("message");
	}
	
	
	/**
	 * 删除外部员工
	 */
	@Permission(module="users", privilege="update")
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {	
		UsersForm formbean = (UsersForm)form;
		usersService.delete((Serializable)formbean.getUserId());
		
		request.setAttribute("message", "删除成功");		
		request.setAttribute("urladdress", SiteUrl.readUrl("sks.privilege.users.list"));
		return mapping.findForward("message");
	}
	
	/**
	 * 启用或禁用外部员工
	 */
	@Permission(module="users", privilege="update")
	public ActionForward enable(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UsersForm formbean = (UsersForm) form;
		usersService.enable(formbean.getStatus(), formbean.getUserIds());
		if(formbean.getStatus())
			request.setAttribute("message", "启用成功");
		else
			request.setAttribute("message", "禁用成功");
			
		request.setAttribute("urladdress", SiteUrl.readUrl("sks.privilege.users.list"));
		return mapping.findForward("message");
	}
	
}
